<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Reason;
use App\Models\User;
use Illuminate\Http\Request;
use Validator;

class ReasonController extends Controller {
	private $role_flag;
	private $user;
	public function __construct() {
		if (\Auth::guard('api')->check()) {
			$getUser = \Auth::guard('api')->user();
			$user = User::find($getUser->id);
			if (!$this->getDevice()) {
				//app
				$user->company_id = $user->appcompany_id;
			}
			$this->user = $user;
			$company = \App\Models\Company::find($this->user->company_id);
			$this->role_flag = $company->role_flag;
		}
	}
	public function all(Request $request, $id) {
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '步骤id不能为空']);
		}
		$data = Reason::where(['status' => 0, 'type' => 2, 'step_id' => $id])->get();
		return $this->respond('get_ok', $data);
	}
	//新增保存
	public function add(Request $request) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		$roleType = $request->input('roleType');
		if ($roleType == 1) {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('flow_add') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('flow_add')) {
					return $this->respond('no_auth');
				}
			}
			return $this->respond('op_ok');
		}
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_add') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_add')) {
				return $this->respond('no_auth');
			}
		}
		$validator = Validator::make($request->all(), Reason::$rules, Reason::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$m = new Reason();
		$step_id = $request->input('stepId');
		$name = $request->input('name');
		$cid = $request->input('cid');
		if ($cid != 2) {
			return response()->json(['code' => 500, 'msg' => '该结果下面不能添加原因']);
		}
		$data = $m::where(['name' => $name, 'type' => 2, 'step_id' => $step_id, 'status' => 0])->first();
		if (!empty($data)) {
			return response()->json(['code' => 500, 'msg' => '原因不能重复']);
		}
		$id = \DB::table('reasons')->insertGetId(['name' => $name, 'step_id' => $step_id, 'created_at' => time(), 'updated_at' => time()]);
		if ($id) {
			return $this->respond('add_ok', Reason::find($id));
		}
		return $this->respond('add_error');
	}
	//更新保存
	public function put(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_add') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_add')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '原因id不能为空']);
		}
		$validator = Validator::make($request->all(),
			['name' => 'required|max:255'],
			['name.required' => '原因名称必须填写', 'name.max' => '原因名称最多255个字符']);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$m = new Reason();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$name = $request->input('name');
		$data = $m::where(['name' => $name, 'type' => 2, 'step_id' => $model->step_id, 'status' => 0])->where('id', '!=', $id)->first();
		if (!empty($data)) {
			return response()->json(['code' => 500, 'msg' => '原因不能重复']);
		}
		$model->name = $name;
		$result = $model->save();
		if ($result) {
			return $this->respond('update_ok');
		}
		return $this->respond('update_error');
	}
	//删除步骤
	public function remove(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_add') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_add')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '原因id不能为空']);
		}
		$m = new Reason();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$model->status = 1;
		$result = $model->save();
		if ($result) {
			return $this->respond('delete_ok');
		}
		return $this->respond('delete_error');
	}
}
